/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package controller;

import com.thoughtworks.xstream.XStream;
import entity.Exampaper;
import entity.Examquestion;
import entity.bean.ExamquestionVo;
import entity.bean.Examquestions;
import entity.bean.InputAnswer;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import service.ExamService;

@Controller
public class PracticeAction {

	@Resource
	private ExamService examService;

	@RequestMapping(value = { "addpractice" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	public String addvoca(Model model, Exampaper exampaper,
			@RequestParam("file") MultipartFile file) {
		XStream stream = new XStream();

		stream.processAnnotations(new Class[] { Examquestion.class,
				Examquestions.class });
		Examquestions examquestions = null;
		try {
			examquestions = (Examquestions) stream.fromXML(file
					.getInputStream());
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		System.out.println(examquestions);
		try {
			this.examService.savePaper(exampaper, examquestions);
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("result", "false");
			return "jsonlist";
		}
		model.addAttribute("result", "true");
		return "jsonlist";
	}

	@RequestMapping(value = { "docheck" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	public String docheck(Model model, HttpServletRequest request, Long paperid) {
		ExamquestionVo examquestionVo = new ExamquestionVo();
		examquestionVo.setPaper(this.examService.getPaperByPaperId(paperid));
		examquestionVo.setQuests(this.examService.getQuestByPaperId(paperid));
		List inputAnswers = new ArrayList();
		InputAnswer input = null;
		int numoffalse = 0;
		Map map = request.getParameterMap();
		Set set = map.keySet();
		Iterator it = set.iterator();

		while (it.hasNext()) {
			String key = (String) it.next();
			if (key.startsWith("choice")) {
				input = new InputAnswer();
				String answer = ((String[]) map.get(key))[0];
				Long questid = Long.valueOf(Long.parseLong(key.substring(6)));
				boolean istrue = true;
				Examquestion exam;
				for (Iterator localIterator1 = examquestionVo.getQuests()
						.iterator(); localIterator1.hasNext();) {
					exam = (Examquestion) localIterator1.next();
					if (exam.getId() == questid) {
						input.setExamquestion(exam);
						if (!(exam.getAnswer().equals(answer))) {
							istrue = false;
							++numoffalse;
							break;
						}
					}
				}

				input.setInput(answer);
				input.setIstrue(istrue);
			}

			inputAnswers.add(input);
		}
		examquestionVo.setNumoffalse(numoffalse);
		examquestionVo.setNumoftotal(0);
		examquestionVo.setNumoftrue(0);
		examquestionVo.setInputAnswers(inputAnswers);
		model.addAttribute("answers", examquestionVo);
		return "practiceanswer";
	}
}